import base64
import json
import os
import time
from urllib.parse import urlencode
from urllib.request import urlopen

import requests
import re

from PIL import Image
from lxml import etree
from selenium import webdriver

# 获取当前路径
from TodayHistory.util.YDMHTTPDemo3 import YDMHttp

abspath = os.getcwd()

# 将数据写入指定文件
def write_to_file(list, file_path):
    output = open(file_path, 'w')
    for i in range(len(list)):
        output.write(str(list[i]) + '\n')
    print(file_path + ' -> 写入成功')
    output.close()

# 更新视频解析网站的哈希值
def update_parsevideo_hash():
    html = get_one_page('https://pocket.mynatapp.cc')
    pattern = '[\s\S]*?var hash = "([\s\S]*?)"[\s\S]*?'
    old_hash = ''
    for tmp_hash in parse_one_page(html, pattern):
        old_hash = tmp_hash
    html = get_one_page('https://www.parsevideo.com/')
    pattern = '[\s\S]*?var hash = "([\s\S]*?)"[\s\S]*?'
    new_hash = ''
    for tmp_hash in parse_one_page(html, pattern):
        new_hash = tmp_hash
    html = get_one_page('https://pocket.mynatapp.cc')
    html = html.replace('var hash = "' + old_hash + '";', 'var hash = "' + new_hash + '";')
    with open('../../../Web/PocketFilm/views/index.html', 'w') as f:
        f.write(html)

# 解决视频解析时的验证码问题
def solve_parsevideo_captche(url='https://pocket.mynatapp.cc/#https://v.youku.com/v_show/id_XMzc5OTM0OTAyMA==.html'):
    driver = get_driver(1)
    driver.maximize_window()
    driver.get(url)
    html = driver.page_source
    if 'style="margin-bottom: 15px;display: none"' in html:
        driver.find_element_by_id('url_submit_button').click()
        time.sleep(2)
        element = driver.find_element_by_id('captcha_img')
        capture(driver, element)
        code = captcha()
        driver.find_element_by_id('captcha_code').send_keys(code)
        driver.find_element_by_id('captcha_sbumit').click()
        time.sleep(2)
        driver.refresh()
    else:
        print('当前不用输入验证码')
    driver.quit()

# 裁剪指定元素
def capture(driver, element, image_path=abspath + '/image', image_name='captcha'):
    """
    截图,指定元素图片
    :param element: 元素对象
    :return: 无
    """
    """图片路径"""
    timestrmap = time.strftime('%Y%m%d_%H.%M.%S')
    imgPath = os.path.join(image_path, '%s.png' % image_name)

    """截图，获取元素坐标"""
    driver.save_screenshot(imgPath)
    left = element.location['x'] + 66
    top = element.location['y'] + 417
    elementWidth = left + element.size['width'] + 60
    elementHeight = top + element.size['height'] + 20

    picture = Image.open(imgPath)
    picture = picture.crop((left, top, elementWidth, elementHeight))
    timestrmap = time.strftime('%Y%m%d_%H.%M.%S')
    imgPath = os.path.join(image_path, '%s.png' % image_name)
    picture.save(imgPath)
    print('元素图标保存位置 -> ' + image_path + '/' + image_name)

# 识别验证码
def captcha(image_path=abspath + '/image/captcha.png'):
    # 用户名
    username = 'Grayson_WP'
    # 密码
    password = 'weipeng185261'
    # 软件ＩＤ，开发者分成必要参数。登录开发者后台【我的软件】获得！
    appid = 7961
    # 软件密钥，开发者分成必要参数。登录开发者后台【我的软件】获得！
    appkey = 'f8c23d784f261f08f028ada4c07fa36b'
    # 图片文件
    filename = image_path
    # 验证码类型，# 例：1004表示4位字母数字，不同类型收费不同。请准确填写，否则影响识别率。在此查询所有类型 http://www.yundama.com/price.html
    codetype = 1004
    # 超时时间，秒
    timeout = 60
    # 检查
    if (username == 'username'):
        return None
        print('请设置好相关参数再测试')
    else:
        # 初始化
        yundama = YDMHttp(username, password, appid, appkey)
        # 登陆云打码
        uid = yundama.login();
        print('uid: %s' % uid)
        # 查询余额
        balance = yundama.balance();
        print('balance: %s' % balance)
        # 开始识别，图片路径，验证码类型ID，超时时间（秒），识别结果
        cid, result = yundama.decode(filename, codetype, timeout);
        print('cid: %s, result: %s' % (cid, result))
        return result

# 获取一个页面的源代码
def get_one_page(url, encode='utf-8'):
    if encode == None:
        encode = 'utf-8'
    response = get_response(url)
    response.encoding = encode
    if response.status_code == 200:
        return response.text
    return None

# 根据 url 获取响应数据
def get_response(url):
    ua_header = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'
    }
    return requests.get(url, headers=ua_header)

# 解析一个页面的信息
def parse_one_page(html, pattern):
    pattern = re.compile(pattern)
    try:
        items = re.findall(pattern, html)
        for item in items:
            yield item
    except:
        yield ()

# 通过xpath解析一个页面的信息
def parse_one_page_with_xpath(html, xpath_pattern):
    return etree.HTML(html)

# 获取视频解析后的地址
def get_movie_parse_url(url):
    driver = get_driver()
    driver.get(url)
    data = driver.execute_script('return parent.now')
    return data

# 获取 web 驱动
def get_driver(type=0):
    # PhantomJS
    if type == 0:
        driver = webdriver.PhantomJS(
            executable_path=abspath + '/phantomjs/bin/phantomjs')
            # executable_path='/usr/local/software/phantomjs-2.1.1-linux-x86_64/bin/phantomjs')
    # Chrome
    if type == 1:
        # 加启动配置
        option = webdriver.ChromeOptions()
        # option.add_argument('headless')
        # 打开Chrome浏览器
        driver = webdriver.Chrome(abspath + '/webdriver/chromedriver/chromedriver', chrome_options=option)
    return driver

# 获取数组中的第一个元素
def get_first_item(arr):
    count = 0
    for item in arr:
        if count == 0:
            return item
        count += 1